package com.kailong.dao;

import com.kailong.pojo.Reserve;
import com.kailong.pojo.Seat;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ManageReserve {
    public static int insert(String sno, String sname, String sclass, String tno, String tfloor, String tsquare, String startTime, String endTime) throws SQLException {
        int count = 0;

        //获取连接对象
        Connection conn = BaseDao.getconn();
        PreparedStatement ps = null;
        String sql = "insert into seatReserve values (?,?,?,?,?,?,?,?)";
        ps = conn.prepareStatement(sql);
        ps.setString(1, sno);
        ps.setString(2, sname);
        ps.setString(3, sclass);
        ps.setString(4, tno);
        ps.setString(5, tfloor);
        ps.setString(6, tsquare);
        ps.setString(7, startTime);
        ps.setString(8, endTime);
        count = ps.executeUpdate();
        BaseDao.closeall(null, ps, conn);
        return count;
    }

    public static int doUpdateState(String tno) {
        Connection conn = BaseDao.getconn();
        int count = 0;
        PreparedStatement ps = null;
        try {
            String sql = "update seat set tstate = ? where tno = ? ";
            String aaa = "已预约";
            ps = conn.prepareStatement(sql);
            ps.setString(1, aaa);
            ps.setString(2, tno);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeall(null, ps, conn);
        }
        return count;
    }

    public static HashMap<String, String> select(String tno) {

        ResultSet rs = null;
        Connection conn = BaseDao.getconn();
        PreparedStatement ps = null;
        String sql = "select startTime,endTime from seatreserve where tno = ? ";
        HashMap<String, String> timeMap = new HashMap<>();
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, tno);
            rs = ps.executeQuery();
            while (rs.next()) {
                String startTime = rs.getString(1);
                String endTime = rs.getString(2);
                timeMap.put(startTime, endTime);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeall(rs, ps, conn);
        }


        return timeMap;
    }

    public static ArrayList<Reserve> selectAll(String sno) {
        ArrayList<Reserve> list = new ArrayList<>();
        //声明结果集
        ResultSet rs = null;
        int count = 0;
        //获取连接对象
        Connection conn = BaseDao.getconn();

        PreparedStatement ps = null;
        try {
            String sql = "select * from seatreserve where sno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, sno);
            rs = ps.executeQuery();
            while (rs.next()) {
                Reserve R = new Reserve(
                        rs.getString(1),
                        rs.getString(2),
                        rs.getString(3),
                        rs.getString(4),
                        rs.getString(5),
                        rs.getString(6),
                        rs.getString(7),
                        rs.getString(8)
                );
                list.add(R);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeall(rs, ps, conn);
        }
        return list;
    }

    /**
     * 取消学生的预约
     *
     * @param sno
     * @param tno
     * @return
     */
    public static int cancelReserve(String sno, String tno) {
        //获取连接对象
        Connection conn = BaseDao.getconn();
        int count = 0;

        PreparedStatement ps = null;
        try {
            String sql = "delete from seatreserve where sno = ? and tno = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, sno);
            ps.setString(2, tno);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeall(null,ps,conn);
        }
        return count;
    }
}
